from selenium import webdriver
from selenium.webdriver.chrome.service import Service as ChromiumService
from webdriver_manager.chrome import ChromeDriverManager
from webdriver_manager.core.os_manager import ChromeType
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
import time
from selenium.webdriver.support import expected_conditions as EC
 

chrome_options = Options()
chrome_options.add_argument('--headless')  # 无头模式
chrome_options.add_argument("--disable-gpu")  # 对于无头模式，禁用 GPU
chrome_options.add_argument('--no-sandbox')
chrome_options.add_argument('--disable-dev-shm-usage')
chrome_options.add_argument('blink-settings=imagesEnabled=false')
chrome_options.add_argument("--dns-prefetch-disable")
driver = webdriver.Chrome(service=ChromiumService(ChromeDriverManager(chrome_type=ChromeType.CHROMIUM).install()),options=chrome_options)

# 打开网页
try:
   driver.get('http://lib-seat.chu.edu.cn/mobile.html#/')
except TimeoutException as ex:
   print(ex.Message)
   driver.navigate().refresh()

 
try:
    password = WebDriverWait(driver, 10).until(
        EC.visibility_of_element_located((By.ID, 'password'))  # 使用元素的定位方式，如 ID
    )
    # 当元素可见时，进行操作
    driver.find_element(By.NAME, 'username').send_keys('21048060')
    password.send_keys('Kk@628628')
    button = driver.find_element(By.CSS_SELECTOR, 'button.auth_login_btn.primary.full_width')
    button.click()
    
finally:
    # 关闭浏览器
    print("登陆成功！")


# driver.find_element(By.NAME, 'username').send_keys('21048060')
# driver.find_element(By.ID, 'password').send_keys('Kk@628628')



try:
    floor = WebDriverWait(driver, 10).until(
        EC.visibility_of_element_located((By.XPATH, "/html/body/div/div[1]/div[3]/div[1]/ul/li/ul/div/li[4]/div"))  # 使用元素的定位方式，如 ID
    )
    floor.click()
    classroom = driver.find_element(By.XPATH, "/html/body/div/div[1]/div[3]/div[1]/ul/li/ul/div/li[4]/ul/div/li[2]")
    driver.execute_script("arguments[0].scrollIntoView();", floor)
    driver.execute_script("window.scrollBy(0,100000)")
    classroom.click()
finally:
    print("打开教室成功")
    
    
    

try:
    seat = WebDriverWait(driver, 10).until(
        EC.visibility_of_element_located((By.XPATH, "//div[@title='710-018']"))  # 使用元素的定位方式，如 ID
    )
    seat.click()
    
finally:
    print("选座成功")


confirm_button = driver.find_element(By.XPATH, "/html/body/div[1]/div[1]/div[3]/div[2]/div[3]/div[2]/div/div[3]/div[1]/div/div[3]/span/button[1]")
time.sleep(0.5)
confirm_button.click()

time.sleep(0.5)
driver.get('http://update.unifound.net/wxnotice/s.aspx?c=100455374_Seat_100456499_1FD')
time.sleep(1)
confirm_signin_button = driver.find_element(By.XPATH, "/html/body/div/div[1]/div/div[2]/div[4]/button")
confirm_signin_button.click()

driver.quit()
